﻿using McsUtility;
using McsUtility.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using TomNet.Redis;

namespace MCSSQL.DBUtility
{
    public class DAL
    {
        public static ResultModel Login(int Type, string LoginName = "", string LoginPass = "", int SMSCode = 0, string Name = "", int Sex = 1, string CardID = "", string PassQuestion1 = "", string PassAnswer1 = "", string PassQuestion2 = "", string PassAnswer2 = "", string PassQuestion3 = "", string PassAnswer3 = "")
        {
            ResultModel rm = new ResultModel();
            rm.success = false;
            rm.message = "失败";
            SqlParameter[] parameters = {
                                            new SqlParameter("@LoginName",SqlDbType.NVarChar,40000),
                                            new SqlParameter("@LoginPass",SqlDbType.NVarChar,40000),
                                            new SqlParameter("@SMSCode",SqlDbType.Int),
                                            new SqlParameter("@Name",SqlDbType.NVarChar,40000),
                                            new SqlParameter("@Sex",SqlDbType.Int),
                                            new SqlParameter("@CardID",SqlDbType.NVarChar,40000),
                                            new SqlParameter("@PassQuestion1",SqlDbType.NVarChar,40000),
                                            new SqlParameter("@PassAnswer1",SqlDbType.NVarChar,40000),
                                            new SqlParameter("@PassQuestion2",SqlDbType.NVarChar,40000),
                                            new SqlParameter("@PassAnswer2",SqlDbType.NVarChar,40000),
                                            new SqlParameter("@PassQuestion3",SqlDbType.NVarChar,40000),
                                            new SqlParameter("@PassAnswer3",SqlDbType.NVarChar,40000),
                                            new SqlParameter("@Type",SqlDbType.Int),
                                            new SqlParameter("@str",SqlDbType.NVarChar,40000),
                                            new SqlParameter("@ReturnValue",SqlDbType.Int)};
            parameters[0].Value = LoginName;
            parameters[1].Value = LoginPass;
            parameters[2].Value = SMSCode;
            parameters[3].Value = Name;
            parameters[4].Value = Sex;
            parameters[5].Value = CardID;
            parameters[6].Value = PassQuestion1;
            parameters[7].Value = PassAnswer1;
            parameters[8].Value = PassQuestion2;
            parameters[9].Value = PassAnswer2;
            parameters[10].Value = PassQuestion3;
            parameters[11].Value = PassAnswer3;
            parameters[12].Value = Type;
            parameters[13].Direction = ParameterDirection.Output;
            parameters[14].Direction = ParameterDirection.ReturnValue;
            DataSet ds = MCSSQL.DBUtility.DbHelperSQL.RunProcedure("Sp_LoginRequest", parameters, "aaa", 3000);
            rm.message= parameters[13].Value.ToString();
            if (Convert.ToInt32(parameters[14].Value) == 1)
            {
                rm.success = true;
                switch (Type)
                {
                    case 1:
                    case 6:
                        LoginResultModel m = new LoginResultModel();
                        m.UserID = Convert.ToInt32(ds.Tables[0].Rows[0]["ID"]);
                        m.Name = Convert.ToString(ds.Tables[0].Rows[0]["Name"]);
                        m.Phone = Convert.ToString(ds.Tables[0].Rows[0]["Phone"]);
                        m.Sex = Convert.ToInt32(ds.Tables[0].Rows[0]["Sex"]);
                        m.Currencys = GetCurrencys(m.UserID);
                        m.Staff = GetStaffModel(m.UserID);
                        rm.data = m;
                        break;
                    case 2:
                    case 3:
                        System.Threading.Tasks.Task task1 = new System.Threading.Tasks.Task(() => SendMessage.SendMessgeYanZhengMa(LoginName, Convert.ToInt32(ds.Tables[0].Rows[0]["SMSCode"]).ToString("000000")));
                        task1.Start();
                        rm.message = "短信发送成功";
                        break;
                    case 4:
                    case 5:
                        break;
                    default:
                        break;
                }
            }
            return rm;
        }
        public static List<CurrencysModel> GetCurrencys(int UserID)
        {
            List<CurrencysModel> currencys = new List<CurrencysModel>();
            DataTable dt = MCSSQL.DBUtility.DbHelperSQL.Query("select * from Currencys").Tables[0];
            foreach (DataRow item in dt.Rows)
            {
                CurrencysModel cm = new CurrencysModel();
                cm.ID = Convert.ToInt32(item["ID"]);
                cm.Name = Convert.ToString(item["Name"]);
                cm.Type = Convert.ToInt32(item["Type"]);
                cm.Remark = Convert.ToString(item["Remark"]);
                cm.Wallets = new List<WalletsModel>();
                DataTable wdt = MCSSQL.DBUtility.DbHelperSQL.Query("select * from Wallets where IsDeleted=0 and UserID=" + UserID + " and CurrencysID=" + cm.ID).Tables[0];
                foreach (DataRow witem in wdt.Rows)
                {
                    WalletsModel wm = new WalletsModel();
                    wm.ID = Convert.ToInt32(witem["ID"]);
                    wm.Address = Convert.ToString(witem["Address"]);
                    wm.Remark = Convert.ToString(witem["Remark"]);
                    cm.Wallets.Add(wm);
                }
                cm.OrePools = new List<WalletsModel>();
                DataTable odt = MCSSQL.DBUtility.DbHelperSQL.Query("select * from OrePools where IsDeleted=0 and UserID=" + UserID + " and CurrencysID=" + cm.ID).Tables[0];
                foreach (DataRow oitem in odt.Rows)
                {
                    WalletsModel om = new WalletsModel();
                    om.ID = Convert.ToInt32(oitem["ID"]);
                    om.Address = Convert.ToString(oitem["Address"]);
                    om.Remark = Convert.ToString(oitem["Remark"]);
                    cm.OrePools.Add(om);
                }
                currencys.Add(cm);
            }
            return currencys;
        }
        public static ResultModel WalletsRequest(int Type, int ID = 0, int UserID = 0, int CurrencysID = 0, string Address = "", string Remark = "")
        {
            ResultModel rm = new ResultModel();
            rm.success = false;
            rm.message = "失败";
            SqlParameter[] parameters = {
                                            new SqlParameter("@ID",SqlDbType.Int),
                                            new SqlParameter("@UserID",SqlDbType.Int),
                                            new SqlParameter("@CurrencysID",SqlDbType.Int),
                                            new SqlParameter("@Address",SqlDbType.NVarChar,40000),
                                            new SqlParameter("@Remark",SqlDbType.NVarChar,40000),
                                            new SqlParameter("@Type",SqlDbType.Int),
                                            new SqlParameter("@str",SqlDbType.NVarChar,40000),
                                            new SqlParameter("@ReturnValue",SqlDbType.Int)};
            parameters[0].Value = ID;
            parameters[1].Value = UserID;
            parameters[2].Value = CurrencysID;
            parameters[3].Value = Address;
            parameters[4].Value = Remark;
            parameters[5].Value = Type;
            parameters[6].Direction = ParameterDirection.Output;
            parameters[7].Direction = ParameterDirection.ReturnValue;
            DataSet ds = MCSSQL.DBUtility.DbHelperSQL.RunProcedure("Sp_WalletsRequest", parameters, "aaa", 3000);
            rm.message = parameters[6].Value.ToString();
            if (Convert.ToInt32(parameters[7].Value) == 1)
            {
                rm.success = true;
            }
            return rm;
        }
        public static ResultModel RemarkRequest(int Type, int ID = 0, int UserID = 0, int MinerID = 0, string Remark = "")
        {
            ResultModel rm = new ResultModel();
            rm.success = false;
            rm.message = "失败";
            SqlParameter[] parameters = {
                                            new SqlParameter("@ID",SqlDbType.Int),
                                            new SqlParameter("@UserID",SqlDbType.Int),
                                            new SqlParameter("@MinerID",SqlDbType.Int),
                                            new SqlParameter("@Remark",SqlDbType.NVarChar,40000),
                                            new SqlParameter("@Type",SqlDbType.Int),
                                            new SqlParameter("@str",SqlDbType.NVarChar,40000),
                                            new SqlParameter("@ReturnValue",SqlDbType.Int)};
            parameters[0].Value = ID;
            parameters[1].Value = UserID;
            parameters[2].Value = MinerID;
            parameters[3].Value = Remark;
            parameters[4].Value = Type;
            parameters[5].Direction = ParameterDirection.Output;
            parameters[6].Direction = ParameterDirection.ReturnValue;
            DataSet ds = MCSSQL.DBUtility.DbHelperSQL.RunProcedure("Sp_RemarkRequest", parameters, "aaa", 3000);
            rm.message = parameters[5].Value.ToString();
            if (Convert.ToInt32(parameters[6].Value) == 1)
            {
                rm.success = true;
            }
            return rm;
        }
        public static ResultModel GetMiners(int UserID, int State,string Currency,string OrePool,string Wallet, int PageIndex, int PageRows)
        {
            ResultModel rm = new ResultModel();
            rm.success = true;
            rm.message = "成功";
           // RedisClient redis = new RedisClient();
            MinersResponseModel mr = new MinersResponseModel();
            List<MinersModel> l = new List<MinersModel>();
            DataTable dt = DbHelperSQL.Query("select * from dbo.Fn_Table_Miners(" + UserID + "," + State + ",'" + Currency + "','" + OrePool + "','" + Wallet + "'," + PageIndex + "," + PageRows + ")").Tables[0];
            foreach (DataRow item in dt.Rows)
            {
                MinersModel m = new MinersModel();
                m.AutoName = Convert.ToBoolean(item["AutoName"]);
                m.Dcri = Convert.ToString(item["Dcri"]);
                m.FirstCoin = Convert.ToString(item["FirstCoin"]);
                m.ID = Convert.ToInt32(item["ID"]);
                m.MacAddress = Convert.ToString(item["MacAddress"]);
                m.FirstPool = Convert.ToString(item["FirstPool"]);
                m.FirstWallet = Convert.ToString(item["FirstWallet"]);
                m.MaxFan = Convert.ToString(item["MaxFan"]);
                m.MinerName = Convert.ToString(item["MinerName"]);
                m.MinFan = Convert.ToString(item["MinFan"]);
                m.Second = Convert.ToBoolean(item["Second"]);
                m.SecondCoin = Convert.ToString(item["SecondCoin"]);
                m.SecondPool = Convert.ToString(item["SecondPool"]);
                m.SecondWallet = Convert.ToString(item["SecondWallet"]);
                m.State = Convert.ToInt32(item["State"]);
                m.StateTime = Convert.ToDateTime(item["StateTime"]);
                m.UserID = Convert.ToInt32(item["UserID"]);
                m.RedisData = "";// redis.StringGet(Convert.ToString(item["ID"]));
                m.AutoRun = Convert.ToBoolean(item["AutoRun"]);
                m.AutoUpdate = Convert.ToBoolean(item["AutoUpdate"]);
                m.AdvParams = Convert.ToString(item["AdvParams"]);
                m.BackupPool = Convert.ToString(item["BackupPool"]);
                m.HighTempStrategy = (HighTempStrategy)Convert.ToInt32(item["HighTempStrategy"]);
                m.CoreType = (CoreType)Convert.ToInt32(item["CoreType"]);
                m.MaxTemp = Convert.ToString(item["MaxTemp"]);
                m.MinTemp = Convert.ToString(item["MinTemp"]);
                m.Eres = Convert.ToString(item["Eres"]);
                m.MinSpeed = Convert.ToString(item["MinSpeed"]);
                m.HighTempWarn = Convert.ToBoolean(item["HighTempWarn"]);
                m.LowStrengthWarn = Convert.ToBoolean(item["LowStrengthWarn"]);
                m.Version = Convert.ToString(item["Version"]);
                l.Add(m);
            }
            mr.Miners = l;
            mr.TotalRows = Convert.ToInt32(DbHelperSQL.GetSingle("select dbo.Fn_Value_MinersCount(" + UserID + "," + State + ",'" + Currency + "','" + OrePool + "','" + Wallet + "')"));
            mr.Rows = Convert.ToInt32(DbHelperSQL.GetSingle("select dbo.Fn_Value_MinersCount(" + UserID + ",-1,'','','')"));
            mr.OnLineRows = Convert.ToInt32(DbHelperSQL.GetSingle("select dbo.Fn_Value_MinersCount(" + UserID + ",1,'','','')+dbo.Fn_Value_MinersCount(" + UserID + ",2,'','','')"));
            mr.WarnRows = 0;
            rm.data = mr;
            return rm;
        }
        public static ResultModel MinersRequest(int Type, int ID = 0, int UserID = 0, string MacAddress = "", bool AutoName = false, bool AutoRun = false, bool AutoUpdate = false, string MinerName = "", int State = 0, string FirstCoin = "", string SecondCoin = "", string FirstPool = "", string SecondPool = "", string FirstWallet = "", string SecondWallet = "", string MinFan = "", string MaxFan = "", string Dcri = "", bool Second = false, string AdvParams = "",string BackupPool="",string MinTemp= "",string  MaxTemp="",string Eres= "",string MinSpeed= "",int HighTempStrategy= 0,bool HighTempWarn= false,bool LowStrengthWarn=false,int CoreType= 0, string Version = "")
        {
            ResultModel rm = new ResultModel();
            rm.success = false;
            rm.message = "失败";
            SqlParameter[] parameters = {
                                            new SqlParameter("@ID",SqlDbType.Int),
                                            new SqlParameter("@UserID",SqlDbType.Int),
                                            new SqlParameter("@MacAddress",SqlDbType.NVarChar,40000),
                                            new SqlParameter("@AutoName",SqlDbType.Bit),
                                            new SqlParameter("@AutoRun",SqlDbType.Bit),
                                            new SqlParameter("@AutoUpdate",SqlDbType.Bit),
                                            new SqlParameter("@MinerName",SqlDbType.NVarChar,40000),
                                            new SqlParameter("@State",SqlDbType.Int),
                                            new SqlParameter("@FirstCoin",SqlDbType.NVarChar,40000),
                                            new SqlParameter("@SecondCoin",SqlDbType.NVarChar,40000),
                                            new SqlParameter("@FirstPool",SqlDbType.NVarChar,40000),
                                            new SqlParameter("@SecondPool",SqlDbType.NVarChar,40000),
                                            new SqlParameter("@FirstWallet",SqlDbType.NVarChar,40000),
                                            new SqlParameter("@SecondWallet",SqlDbType.NVarChar,40000),
                                            new SqlParameter("@MinFan",SqlDbType.NVarChar,40000),
                                            new SqlParameter("@MaxFan",SqlDbType.NVarChar,40000),
                                            new SqlParameter("@Dcri",SqlDbType.NVarChar,40000),
                                            new SqlParameter("@Second",SqlDbType.Bit),
                                            new SqlParameter("@AdvParams",SqlDbType.NVarChar,40000),
                                            new SqlParameter("@BackupPool",SqlDbType.NVarChar,40000),
                                            new SqlParameter("@MinTemp",SqlDbType.NVarChar,40000),
                                            new SqlParameter("@MaxTemp",SqlDbType.NVarChar,40000),
                                            new SqlParameter("@Eres",SqlDbType.NVarChar,40000),
                                            new SqlParameter("@MinSpeed",SqlDbType.NVarChar,40000),
                                            new SqlParameter("@HighTempStrategy",SqlDbType.Int),
                                            new SqlParameter("@HighTempWarn",SqlDbType.Bit),
                                            new SqlParameter("@LowStrengthWarn",SqlDbType.Bit),
                                            new SqlParameter("@CoreType",SqlDbType.Int),
                                            new SqlParameter("@Type",SqlDbType.Int),
                                            new SqlParameter("@str",SqlDbType.NVarChar,40000),
                                            new SqlParameter("@ReturnValue",SqlDbType.Int)};
            parameters[0].Value = ID;
            parameters[1].Value = UserID;
            parameters[2].Value = MacAddress;
            parameters[3].Value = AutoName;
            parameters[4].Value = AutoRun;
            parameters[5].Value = AutoUpdate;
            parameters[6].Value = MinerName;
            parameters[7].Value = State;
            parameters[8].Value = FirstCoin;
            parameters[9].Value = SecondCoin;
            parameters[10].Value = FirstPool;
            parameters[11].Value = SecondPool;
            parameters[12].Value = FirstWallet;
            parameters[13].Value = SecondWallet;
            parameters[14].Value = MinFan;
            parameters[15].Value = MaxFan;
            parameters[16].Value = Dcri;
            parameters[17].Value = Second;
            parameters[18].Value = AdvParams;
            parameters[19].Value = BackupPool;
            parameters[20].Value = MinTemp;
            parameters[21].Value = MaxTemp;
            if (Type == 1)
            {
                parameters[22].Value = Version;
            }
            else
            {
                parameters[22].Value = Eres;
            }
            parameters[23].Value = MinSpeed;
            parameters[24].Value = HighTempStrategy;
            parameters[25].Value = HighTempWarn;
            parameters[26].Value = LowStrengthWarn;
            parameters[27].Value = CoreType;

            parameters[28].Value = Type;
            parameters[29].Direction = ParameterDirection.Output;
            parameters[30].Direction = ParameterDirection.ReturnValue;
            DataSet ds = MCSSQL.DBUtility.DbHelperSQL.RunProcedure("Sp_MinersRequest", parameters, "aaa", 3000);
            rm.message = parameters[29].Value.ToString();
            if (Convert.ToInt32(parameters[30].Value) == 1)
            {
                rm.success = true;
                switch (Type)
                {
                    case 1:
                        rm.data = Convert.ToInt32(ds.Tables[0].Rows[0]["ID"]);
                        break;
                    default:
                        break;
                }
            }
            return rm;
        }
        public static MinerWalletModel GetMinerWalletModel(int MinerID)
        {
            MinerWalletModel m = new MinerWalletModel();
            DataTable dt = MCSSQL.DBUtility.DbHelperSQL.Query("select Miners.ID as MinerID,Miners.MinerName,Miners.UserID,Users.Name as UserName,Users.Phone as UserPhone,Miners.FirstWallet as WalletAddress from Miners inner join Users on Miners.UserID=Users.ID where Miners.ID=" + MinerID).Tables[0];
            foreach (DataRow item in dt.Rows)
            {
                m.MinerID = Convert.ToInt32(item["MinerID"]);
                m.MinerName = Convert.ToString(item["MinerName"]);
                m.UserID = Convert.ToInt32(item["UserID"]);
                m.UserName = Convert.ToString(item["UserName"]);
                m.UserPhone = Convert.ToString(item["UserPhone"]);
                m.WalletAddress = Convert.ToString(item["WalletAddress"]);
            }
            return m;
        }
        public static bool AddMinerLog(int MinerID,string Logs)
        {
            return MCSSQL.DBUtility.DbHelperSQL.ExecuteSql("insert into MinerLogs (MinerID,Logs) values (" + MinerID + ",'" + Logs + "')") > 0;
        }
        public static ResultModel GetMinerModel(int MinerID)
        {
            ResultModel rm = new ResultModel();
            rm.success = true;
            rm.message = "成功";
            // RedisClient redis = new RedisClient();
            DataTable dt = DbHelperSQL.Query("select * from Miners where ID=" + MinerID).Tables[0];
            MinersModel m = new MinersModel();
            foreach (DataRow item in dt.Rows)
            {
                m.AutoName = Convert.ToBoolean(item["AutoName"]);
                m.Dcri = Convert.ToString(item["Dcri"]);
                m.FirstCoin = Convert.ToString(item["FirstCoin"]);
                m.ID = Convert.ToInt32(item["ID"]);
                m.MacAddress = Convert.ToString(item["MacAddress"]);
                m.FirstPool = Convert.ToString(item["FirstPool"]);
                m.FirstWallet = Convert.ToString(item["FirstWallet"]);
                m.MaxFan = Convert.ToString(item["MaxFan"]);
                m.MinerName = Convert.ToString(item["MinerName"]);
                m.MinFan = Convert.ToString(item["MinFan"]);
                m.Second = Convert.ToBoolean(item["Second"]);
                m.SecondCoin = Convert.ToString(item["SecondCoin"]);
                m.SecondPool = Convert.ToString(item["SecondPool"]);
                m.SecondWallet = Convert.ToString(item["SecondWallet"]);
                m.State = Convert.ToInt32(item["State"]);
                m.StateTime = Convert.ToDateTime(item["StateTime"]);
                m.UserID = Convert.ToInt32(item["UserID"]);
                m.RedisData = "";// redis.StringGet(Convert.ToString(item["ID"]));
                m.AutoRun = Convert.ToBoolean(item["AutoRun"]);
                m.AutoUpdate = Convert.ToBoolean(item["AutoUpdate"]);
                m.AdvParams = Convert.ToString(item["AdvParams"]);
                m.BackupPool = Convert.ToString(item["BackupPool"]);
                m.HighTempStrategy = (HighTempStrategy)Convert.ToInt32(item["HighTempStrategy"]);
                m.CoreType = (CoreType)Convert.ToInt32(item["CoreType"]);
                m.MaxTemp = Convert.ToString(item["MaxTemp"]);
                m.MinTemp = Convert.ToString(item["MinTemp"]);
                m.Eres = Convert.ToString(item["Eres"]);
                m.MinSpeed = Convert.ToString(item["MinSpeed"]);
                m.HighTempWarn = Convert.ToBoolean(item["HighTempWarn"]);
                m.LowStrengthWarn = Convert.ToBoolean(item["LowStrengthWarn"]);
            }
            rm.data = m;
            return rm;
        }
        public static ResultModel GetLogs(int MinerID, int PageIndex, int PageRows)
        {
            ResultModel rm = new ResultModel();
            rm.success = true;
            rm.message = "成功";
            // RedisClient redis = new RedisClient();
            MinerLogsResponseModel mr = new MinerLogsResponseModel();
            List<MinerLogsModel> l = new List<MinerLogsModel>();
            DataTable dt = DbHelperSQL.Query("select * from dbo.Fn_Table_MinerLogs(" + MinerID + "," + PageIndex + "," + PageRows + ")").Tables[0];
            foreach (DataRow item in dt.Rows)
            {
                MinerLogsModel m = new MinerLogsModel();
                m.CreateTime = Convert.ToDateTime(item["CreateTime"]);
                m.Logs = Convert.ToString(item["Logs"]);
                l.Add(m);
            }
            mr.Logs = l;
            mr.TotalRows = Convert.ToInt32(DbHelperSQL.GetSingle("select dbo.Fn_Value_MinerLogsCount(" + MinerID + ")"));
            rm.data = mr;
            return rm;
        }
        public static ResultModel GetStaffs()
        {
            ResultModel rm = new ResultModel();
            rm.success = true;
            rm.message = "成功";
            // RedisClient redis = new RedisClient();
            List<StaffModel> l = new List<StaffModel>();
            DataTable dt = DbHelperSQL.Query("select * from Staffs where RoleID=2").Tables[0];
            foreach (DataRow item in dt.Rows)
            {
                StaffModel m = new StaffModel();
                m.ID = Convert.ToInt32(item["ID"]);
                m.Name = Convert.ToString(item["Name"]);
                m.QQ1 = Convert.ToString(item["QQ1"]);
                m.QQ2 = Convert.ToString(item["QQ2"]);
                m.QQ3 = Convert.ToString(item["QQ3"]);
                m.WeiXin1 = Convert.ToString(item["WeiXin1"]);
                m.WeiXin2 = Convert.ToString(item["WeiXin2"]);
                m.WeiXin3 = Convert.ToString(item["WeiXin3"]);
                l.Add(m);
            }
            rm.data = l;
            return rm;
        }
        public static StaffModel GetStaffModel(int UserID)
        {
            StaffModel m = new StaffModel();
            DataTable dt = DbHelperSQL.Query("select * from Staffs where ID=(select StaffID from Users where ID=" + UserID + ")").Tables[0];
            foreach (DataRow item in dt.Rows)
            {
                m.ID = Convert.ToInt32(item["ID"]);
                m.Name = Convert.ToString(item["Name"]);
                m.QQ1 = Convert.ToString(item["QQ1"]);
                m.QQ2 = Convert.ToString(item["QQ2"]);
                m.QQ3 = Convert.ToString(item["QQ3"]);
                m.WeiXin1 = Convert.ToString(item["WeiXin1"]);
                m.WeiXin2 = Convert.ToString(item["WeiXin2"]);
                m.WeiXin3 = Convert.ToString(item["WeiXin3"]);
            }
            return m;
        }

    }
}
